import pymysql
from sqlalchemy import create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, Session
from contextlib import contextmanager

pymysql.install_as_MySQLdb()

SQLALCHEMY_DATABASE_URL = "mysql+pymysql://root:121212@127.0.0.1:3306/ihome_fast"

# 创建同步引擎
engine = create_engine(SQLALCHEMY_DATABASE_URL)

SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

Base = declarative_base()


# def create_database():
#     # 在同步上下文中执行表结构创建
#     Base.metadata.create_all(engine)


# ... (其他导入)

# @contextmanager
def get_db():
	connection = engine.connect()
	db = Session(bind=connection)
	try:
		yield db
	finally:
		db.close()
